import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import week6.annotation.ConfigSource;
import week6.annotation.ConfigSourceFactory;
import week6.annotation.ConfigSources;
import week6.annotation.DefaultConfigSourceFactory;

import java.net.URL;

@ConfigSource(resource = "classpath:pro1.properties")
@ConfigSource(resource = "classpath:pro2.properties")
public class ConfigSourceTestClass {
    @BeforeClass
    public static void init() {
        org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.getInstance();
    }

    @Before
    public void getConfigSources() throws Throwable {
        //获取@ConfigSources注解
        ConfigSources configSources = getClass().getAnnotation(ConfigSources.class);
        //获取定义的ConfigSource
        ConfigSource[] sourcesList=configSources.value();
        int i=0;
        for (ConfigSource configSource : sourcesList){
            String resource = configSource.resource();
            URL resourceURL = new URL(resource);
            Class<? extends ConfigSourceFactory> configSourceFactoryClass = configSource.factory();
            if (ConfigSourceFactory.class.equals(configSourceFactoryClass)) {
                configSourceFactoryClass = DefaultConfigSourceFactory.class;
            }

            ConfigSourceFactory configSourceFactory = configSourceFactoryClass.newInstance();
            org.eclipse.microprofile.config.spi.ConfigSource source =
                    configSourceFactory.createConfigSource("", i, resourceURL, "UTF-8");
            System.out.println(source.getProperties());
            i++;
        }
    }

    @Test
    public void test() {

    }
}
